﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DAO;

namespace BLL
{
    public class FeedbackBLL
    {
        TrainingCourseDataContext db = new TrainingCourseDataContext();
        public void SaveFeedback(string userName,string courseID,string [,] feedback)
        {
            // If Pass
            string answer = "";
            string notes="";
            for (int i = 0; i < 8; i++)
            {
                answer +=feedback[0,i]+"\\n";
                notes +=feedback[1, i]+"\\n";
            }

            try
            {
                if (IsExists(userName,courseID))
                {
                    Feedback result = db.Feedbacks.FirstOrDefault(p => p.UserName == userName && p.CourseID == courseID);
                    
                    result.Feedback1 = answer;
                    result.Notes = notes;

                    db.SubmitChanges();
                }
                else
                {
                    Feedback result = new Feedback
                    {
                        UserName = userName,
                        CourseID = courseID,
                        Notes = notes,
                        Feedback1 = answer
                    };

                    

                    db.Feedbacks.InsertOnSubmit(result);
                    db.SubmitChanges();
                }
            }
            catch
            {
                throw;
            }

        }
        public bool IsExists(string userName,string courseID)
        {
            bool flag = false;
            if (db.Feedbacks.FirstOrDefault(p => p.UserName == userName&&p.CourseID==courseID) != null)
                flag = true;
            return flag;
        }

    }
}
